package rmi.client;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

import rmi.server.IServer;
import rmi.utils.common.Configuration;
import rmi.utils.common.RMICommon;



public class RMIClientCalculadora extends RMICommon implements IRMIClientCalculadora {
	private IServer server;
	private String ipServer;
	private static Configuration config;
	
	/**
	 * @return the ipServer
	 */
	public String getIpServer() {
		return ipServer;
	}
	/**
	 * @param ipServer the ipServer to set
	 */
	public void setIpServer(String ipServer) {
		this.ipServer = ipServer;
	}
	/**
	 * @return the rmiName
	 */
	public String getRmiName() {
		return rmiName;
	}
	/**
	 * @param rmiName the rmiName to set
	 */
	public void setRmiName(String rmiName) {
		this.rmiName = rmiName;
	}

	private String rmiName;

	public  RMIClientCalculadora() {
		config = new Configuration();
		this.ipServer = config.getIpServer();
		this.rmiName = config.getRmiName();
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public  RMIClientCalculadora(String ip, String rmiName) {
		this.ipServer = ip;
		this.rmiName = rmiName;
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void init() throws Exception{
		
		InetAddress thisIp = InetAddress.getLocalHost();
		System.out.println("IP:" + thisIp.getHostAddress());
		
		System.setProperty("java.rmi.server.hostname", thisIp.getHostAddress());
        System.setProperty("java.security.policy", "security.policy");
        System.setProperty("java.rmi.server.ignoreStubClasses", "true");
		computeCodeBase();
		System.setSecurityManager(new RMISecurityManager());
		//printUsage();
		getServer();
		
				
		
	}
	
	private void getServer(){
		
		try {
			
			Registry registry = LocateRegistry.getRegistry(this.ipServer, 1099);
			this.server = (IServer) registry.lookup(this.rmiName);
			/*Registry registry = ConectionRMI.getConnectionLocateRegistry();
			this.server = (IServer) registry.lookup("WarServer");*/
			//System.out.println("calc:"+server.calcular("+",2d, 2d));
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("erro na conexao com a classe servidor");
		} 
	}
	public double soma(double x, double y){
		double result = 0d;
		try {
			result =  server.calcular("+", x, y);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public double subtracao(double x, double y){
		double result = 0d;
		try {
			result =  server.calcular("-", x, y);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public double divisao(double x, double y){
		double result = 0d;
		try {
			result =  server.calcular("/", x, y);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public double multiplicacao(double x, double y){
		double result = 0d;
		try {
			result =  server.calcular("*", x, y);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private static void printUsage() {
		  OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
		  for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
		    method.setAccessible(true);
		    if (method.getName().startsWith("get") 
		        && Modifier.isPublic(method.getModifiers())) {
		            Object value;
		        try {
		            value = method.invoke(operatingSystemMXBean);
		        } catch (Exception e) {
		            value = e;
		        } // try
		        System.out.println(method.getName() + " = " + value);
		    } // if
		  } // for
		}
	
	public static void main(String args[]){
		
			config = new Configuration();
			RMIClientCalculadora v = new RMIClientCalculadora();
			System.out.println(v.soma(2.0, 5.0));
		
		
		
	}
}
